package com.demo03_feed.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

@Component
public class NacosShutdownListener implements ApplicationListener<ContextClosedEvent> {

    @Autowired(required = false)
    private ServiceRegistry<Registration> serviceRegistry;

    @Autowired(required = false)
    private Registration registration;

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        if (serviceRegistry != null && registration != null) {
            serviceRegistry.deregister(registration);
        }
    }
}